package grib2handler.bufr;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import grib2handler.bufr.http.HTTPDown;
import grib2handler.bufr.pass.BufrPass;
import grib2handler.util.PropertiesUtil;

public class BufrDownTask  implements Job  {
	private final static Logger log = Logger.getLogger(BufrDownTask.class);
	private static ThreadPoolExecutor pool =  null;
	private static String urlPath = "http://128.117.114.119/native/bufr/";
	private static String filePath = PropertiesUtil.getConfig().getString("bufrOutFile") + "/";
	private static String bufrXmlPath = PropertiesUtil.getConfig().getString("bufrxml") + "/";
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		log.info("bufr定时任务开始");
		//查看最新时次数据，一个小时定时查一次
		//5分钟检查一次，检查10次
		for (int j = 0; j < 10; j++) {
			loadData(1);
			log.info("bufr新数据没有出现休眠一段时间");
			//睡眠5分钟
			try {
				Thread.sleep(5 * 60 * 1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		log.info("bufr定时任务完成");
	}
	
	/**
	 * 加载数据
	 */
	public static void loadData(int downNumber) { 
		log.info("加载bufr数据");
		//查看最新时次数据
		String s = HTTPDown.getHTML(urlPath + "Cimulonimbus/");
		log.info("获取bufr列表数据结束");
		List<String> names = new ArrayList<String>();
		try {
			Parser parser = new Parser(s);
			NodeList nodes = parser.extractAllNodesThatMatch(new TagNameFilter("a"));
			Node[] links = nodes.toNodeArray();
			String regEx = ".*(\\d{8})_(\\d{2})\\d{2}.bufr\"";
			Pattern pat = Pattern.compile(regEx);
			//取出最近的10个时次
			int i = links.length > downNumber ? links.length - downNumber : 0;
			for (; i < links.length; i++) {
				Node linkTag = links[i];
				Matcher mat = pat.matcher(linkTag.getText());
				boolean rs = mat.find();
				if(rs) {
					names.add(mat.group(1) + mat.group(2));
				}
			}
			//根据要下载的模板，进行下载解析
			List<Object> bufrNames =  PropertiesUtil.getConfig().getList("bufrNames");
			for (String shiCi : names) {
				final String date = shiCi.substring(0, 8);
				final String time = shiCi.substring(8, 10);
				for (Object bufrName : bufrNames) {
					final String name = bufrName.toString();
					final String fileName = name + "_" + date + "_" + time + "00.bufr";
					//线程下载数据
					if(pool != null) {
						pool.execute(new Runnable() {
							@Override
							public void run() {
								int downStatus = HTTPDown.httpDownload(urlPath + name + "/" + fileName, filePath + date
										+ time + "/" + fileName);
								//下载成功解析数据
								if(downStatus == HTTPDown.DOWN_SUCCESS) {
									//解析数据，以后可以配置多输出路径
									BufrPass.parseBufrFile(filePath + date + time + "/" + fileName,
											bufrXmlPath + date + time + "/");
								}
							}
						});
					}
					
				}
			}
			
		} catch (Exception e) {
			log.error("加载bufr数据出错" + e.getMessage());
		}
	}
	public static ThreadPoolExecutor getPool() {
		return pool;
	}

	public static void setPool(ThreadPoolExecutor pool) {
		BufrDownTask.pool = pool;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		BufrDownTask.setPool(new ThreadPoolExecutor(4, 20, 10, TimeUnit.SECONDS,
	    		new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.AbortPolicy()));
	}

}
